package servlet.controller;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import servlet.bean.UserBean;
import servlet.model.ServiceFactory;
import servlet.publicUtils.MyUtils;

public class LoginServlet extends HttpServlet {

	private static final long serialVersionUID = 1L;
	

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		System.out.println("接收到客户端请求");
		//req.setCharacterEncoding("UTF-8");
		String strUserName=req.getParameter("username");
		String strUserPsw=req.getParameter("password");
		//此种和下面两种都可以UserBean userB=new UserBean();
		UserBean userB = (UserBean) MyUtils.getNewInstance(UserBean.class);
		userB.setUsername(strUserName);
		userB.setUser_psw(strUserPsw);
		
		//boolean isL=ServiceFactory.getLoginServiceInstance().isLogin(userB);
		List<UserBean> lstUser=ServiceFactory.getLoginServiceInstance().isLogin2(userB);
		
		String strResultPage="login.jsp";
		if(lstUser!=null&& lstUser.size()>0){
			strResultPage="logined/logined.jsp";
			
			req.getSession().setAttribute("loginedUserB", lstUser.get(0));
		}
		req.getRequestDispatcher(strResultPage).forward(req, resp);
	}


	@Override
	public void destroy() {
		// TODO Auto-generated method stub
		super.destroy();
	}


	@Override
	public void init() throws ServletException {
		// TODO Auto-generated method stub
		super.init();
	}

	

}
